package com.tarena.lbs.activity.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Slf4j
@Repository
public class ActivityRepositoryImpl implements ActivityRepository {
    @Autowired
    private ActivityMapper activityMapper;


    /*
    * 活动详情
    * */
    @Override
    public ActivityPO getActivityById(Integer id) {
        return activityMapper.selectById(id);
    }

    /*
    * 活动查询列表
    * */
    @Override
    public PageInfo<ActivityPO> pageList(ActivityQuery query) {
        QueryWrapper<ActivityPO> queryWrapper = assembleActivityQuery(query);
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
        List<ActivityPO> pos = activityMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    /*
    * 修改活动状态
    * */
    @Override
    public void updateStatus(Integer id, Integer status) {
        ActivityPO activityPO = new ActivityPO();
        activityPO.setStatus(status);
        activityMapper.update(activityPO, new QueryWrapper<ActivityPO>().eq("id", id));
    }

    /*
    * 删除活动
    * */
    @Override
    public void delete(Integer id) {
        activityMapper.deleteById(id);
    }

    /*
    * 新增活动
    * */
    @Override
    public void saveActivity(ActivityPO activityPO) {
        activityMapper.insert(activityPO);
    }

    @Override
    public void updateActivity(ActivityPO activityPO) {
        activityMapper.updateById(activityPO);
    }

    /*
    * 定时更新活动状态
    * */
    @Override
    public List<ActivityPO> findNeedStatusUpdateActivities(Date date, Date now) {
        Date fiveMinutesLater = DateUtils.addMinutes(now, 5);
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                .or(wq -> wq
                        .eq("status", 0)
                        .between("start_date", now, fiveMinutesLater)
                )
                .or(wq -> wq
                        .eq("status", 1)
                        .between("end_date", now, fiveMinutesLater)
                )
        );
        List<ActivityPO> needCheckActivities = activityMapper.selectList(queryWrapper);
        return needCheckActivities;
    }

    private QueryWrapper<ActivityPO> assembleActivityQuery(ActivityQuery query) {
    QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
    if (StringUtils.isNotBlank(query.getActivityName())){
        queryWrapper.like("activity_name",query.getActivityName());
    }
    if (query.getDeliveryChannel() != null){
        queryWrapper.eq("delivery_channel",query.getDeliveryChannel());
    }
    if (query.getType() != null){
        queryWrapper.eq("type",query.getType());
    }
    if (query.getChannelType() != null){
        queryWrapper.eq("channel_type",query.getChannelType());
    }
    if (query.getStatus() != null){
        queryWrapper.eq("status",query.getStatus());
    }
    if (query.getBusinessId() != null){
        queryWrapper.eq("business_id",query.getBusinessId());
    }
    // 筛选startDate和endDate之间的数据
    if (query.getStartDate() != null) {
        queryWrapper.ge("start_date", query.getStartDate());
    }
    if (query.getEndDate() != null) {
        queryWrapper.le("end_date", query.getEndDate());
    }
    return queryWrapper;
    }
}
